# -*- coding: utf-8 -*-
'''
@Created on: 2011-7-27
@version: 2011.7.27
@summary: Definition of Actions class
'''

from util.datamap import xpathMap
import time

class Action(object):
    '''
    Base action class on web site. Called for Step class.
    '''
    
    def __init__(self, name, value=None):
        '''
        Constructor
        Keyword arguments:
        name -- action name, e.g. Feed Input
        sele -- selenium object
        '''
        self.name = name
        self.value = value
        self.sele = None
        self.xpath = xpathMap
        self.flag = True
        self.imgpath = ""
    
    def setup(self, sele):
        self.sele = sele
        
    def do(self):
        if (self.flag):
            print 'action -- do'
        '''
        Execute action.
        Keyword arguments:
        value -- action test data
        '''
        if self.sele is not None:
            if self.name == 'open':
                self.open()
            elif self.name == 'type':
                self.type()
            elif self.name == 'click':
                self.click()
            elif self.name == 'screencapture':
                return self.scr_capture()
            elif self.name == 'waitforpageload':
                self.wait_for_page_to_load()
            elif self.name == 'verifytext':
                self.verify_text()
            else:
                raise Exception, "Parameter error in do()"
        else:
            pass
        
    def open(self):
        if (self.flag):
            print 'action -- open'
        
        if self.value is not None:
            dest = self.value[0]
        else:
            dest = "/"
        self.sele.open(dest)
        self.sele.window_maximize()
        
    def type(self):
        if (self.flag):
            print 'action -- type'
        xpath = self.xpath[self.value[0]]
        content = self.value[1]
        self.sele.type(xpath, content)
        
    def click(self):
        if (self.flag):
            print 'action -- click'
        xpath = self.xpath[self.value[0]]
        self.sele.click(xpath)
        
    def scr_capture(self):
        if (self.flag):
            print 'action -- start screencapture '
        pic_string = self.sele.capture_entire_page_screenshot_to_string("background=#FFFFFF")
        return pic_string
    
    def wait_for_page_to_load(self):
        if (self.flag):
            print 'action -- wait_for_page_to_load'
        self.sele.wait_for_page_to_load(self.value[0])
        
    def verify_text(self):
        if (self.flag):
            print 'action -- verify_text: ' + str(self.value[0])
        verify_result = self.sele.is_text_present(self.value[0])
#        verify_result = False
        if not(verify_result):
            raise Exception, "Verify text failed - text: " + str(self.value[0])
        
    def check(self):
        if (self.flag):
            print 'action -- check' + str(self.value[0])        
        xpath = self.xpath[self.value[0]]
        self.sele.check(xpath)    
    
    def select(self):
        if (self.flag):
            print 'action -- select' + str(self.value[0])
        self.sele.select(self.xpath[self.value[0]],self.value[1])
        
    def fireevent(self):
        if (self.flag):
            print 'action -- fireevent' + str(self.value[0])
        self.sele.fire_event(self.xpath[self.value[0]],'blur')   
        
    def upload(self):
        if (self.flag):
            print 'action -- upload' + str(self.value[0])
        path=(self.imgpath + 'icon.png')
        self.sele.type(self.value[0], path)                      
        